package com.action.mvc.sm.controller;

import com.action.mvc.sm.pojo.Article;
import com.action.mvc.sm.pojo.UserMvc;
import com.action.mvc.sm.service.ArticleService;
import com.action.mvc.sm.service.BooksService;
import com.action.mvc.sm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by Wuxiang on 2017/3/8.
 */
@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private BooksService booksService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        System.out.println("Hello World");
        return "Hello World!!!";
    }

    /**
     * 查询 user_mvc 表数据，并使用 pagehelper 进行分页
     * @return jsp页面
     */
    @RequestMapping("/user")
    public ModelAndView findAll() {
        System.out.println("user");

        ModelAndView modelAndView = new ModelAndView();
        List<UserMvc> userMvcs = userService.findAll();
        modelAndView.setViewName("findUser");
        modelAndView.addObject("users",userMvcs);
        return modelAndView;
    }

    /**
     * 查询 article 表 所有数据
     * 注意：SpringMVC默认没有JSON转换器，需要引入 jackson 进行转换
     * Spring5版本，需要jackson2.9.5以上，低版本会出现 RequestMappingHandlerAdapter 异常
     * @return JSON数据
     */
    @RequestMapping("/article")
    @ResponseBody
    public List<Article> selectAll() {
        System.out.println("article");

        return articleService.selectAll();
    }

    /**
     * 事务
     * 执行多条语句时，需要保持数据的一致性
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    @ResponseBody
    public String test() throws Exception {
        System.out.println("update");
        booksService.update();
        return "update";
    }
}
